<?php

function file_get_between($d_s, $d_e, $key){
    $s = "";

    $d_s = strtotime(str_replace("T"," ",$d_s));
    $d_e = strtotime(str_replace("T"," ",$d_e));
    $d = $d_s;
    $s_h = date("Y-m", $d);
    $s_h_e = date("Y-m", $d_e);
    //send($s_h);
    //send($s_h_e);
    while ($s_h <= $s_h_e) {
        //send($s_h);
        $key = str_replace("/", "", $key);
        $key = str_replace("\\", "", $key);
        $f = getconfig("path") . $s_h . "/" . $key . "_" . $s_h . ".txt";
        $s .= file_get_contents($f);
        $d = strtotime("+1 month", $d);
        $s_h = date("Y-m", $d);
    }
    return $s;
}

function data_split($data, $d_s, $d_e, $v_s, $v_e)
{
    //$data = str_replace(chr(10), ";", $data);
    //echo $data;
    //print_r(explode(chr(10), $data));
    $lab = "";
    $dat = "";
    $ary = explode(chr(10), $data);
    //$dat = count($ary);
    for ($i = 0; $i < count($ary); $i++) {
        $a2 = explode(' ', $ary[$i]);
        try {
            if (!empty($a2) && count($a2) >= 2) {
                // 按开始时间和结束时间过滤
                if ($a2[0] < $d_s || $a2[0] > $d_e)
                    continue;
                if ((!empty($v_s) && (float)$a2[1] < (float)$v_s) || (!empty($v_e) && (float)$a2[1] > (float)$v_e))
                    continue;
                $t1 = $a2[0];
                //$t1 = substr($t1,8,8));
                $t1 = str_replace("T", " ", $t1);
                $lab .= "'" . $t1 . "',";
                $dat .= $a2[1] . ",";
            }
        } catch (Excepthon $e) {
            echo $e;
        }
    }
    //echo $lab;
    //$GLOBALS['lab'] = $lab;
    //$GLOBALS['dat'] = $dat;
    return array($lab, $dat);
}

function getdate_s($t, $d)
{
    $ts = array("-1 hours", "-3 hours", "-6 hours", "-12 hours", "-1 days", "-3 days",
        "-1 weeks", "-1 months", "-1 years");
    $s = $ts[(int)$t - 1];
    $d = strtotime($s, $d);
    return date("Y-m-d", $d) . "T" . date("H:i:00", $d);
}

function getconfig($key)
{
    $xml = simplexml_load_file("web.xml");
    foreach ($xml->children() as $child) {
        if ($child->getName() == $key)
            return $child;
    }
    return "";
}

function getData()
{
    //$id = $_REQUEST["id"];
    $t = $_REQUEST["t"];
    $dt = $_REQUEST["dt"];
    $key = $_REQUEST["key"];
    
    $v_s = null;
    $v_e = null;
    if ( !empty($_REQUEST["v_s"]) ) $v_s = $_REQUEST["v_s"];
    if ( !empty($_REQUEST["v_e"]) ) $v_e = $_REQUEST["v_e"];
    
    $tit = "";
    if ( !empty($_REQUEST["tit"]) ) $tit = $_REQUEST["tit"];

    $d = strtotime($dt);
    $error = "";
    $f = date("Y-m", $d) . "/" . $key . date("_Y-m", $d) . ".txt";
    $f2 = getconfig("path") . $f;
    $f3 = '';
    //$GLOBALS['lab'] = "";
    //$GLOBALS['dat'] = "";
    $lab = "";
    $dat = "";
    if (file_exists($f2)) {
        try {
            $f3 = 'data/' . $f;
            $_SESSION['f'] = $f2;
            //$data = read_file_content($f2);
            //$data = file_get_contents($f2);

            $date_s = getdate_s($t, $d);
            $date_e = date("Y-m-d", $d) . "T" . date("H:i:00", $d);
            //send($date_s);
            //send($date_e);
            $data = file_get_between($date_s, $date_e, $key);

            if ( !empty($_REQUEST["d_s"]) && !empty($_REQUEST["d_e"]) ) {
                $d1 = strtotime($_REQUEST["d_s"]);
                $d2 = strtotime($_REQUEST["d_e"]);
                $date_s = date("Y-m-d", $d1) . "T" . date("H:i:s", $d1);
                $date_e = date("Y-m-d", $d2) . "T" . date("H:i:s", $d2);
            }
            list($lab, $dat) = data_split($data, $date_s, $date_e, $v_s, $v_e);

        } catch (Exception $e) {
            $error = $e;
        }
    } else {
        $error = "文件不存在!(" . $f . ")";  //文件不存在
    }

    //echo $GLOBALS['lab'];
    //echo $d_id.", ".$t.", ".$date_s.", ".$date_e;
    //echo '{"lab":[' . $GLOBALS['lab'] . '],"dat":[' . $GLOBALS['dat'] . '],'
    echo '{"lab":[' . $lab . '],"dat":[' . $dat . '],'
        . '"tit":"' . $tit . '","error":"' . $error . '","f":"' . $f3 . '"}';
}

function getData_Last()
{

    $key = $_REQUEST["key"];
    $div = $_REQUEST["div"];
    
    $error = "";
    $f = date("Y-m") . "/" . $key . date("_Y-m") . ".txt";
    $f2 = getconfig("path") . $f;
    $f3 = '';
    $lab = "";
    $dat = "";   
    if (file_exists($f2)) {
        try {
            $f3 = 'data/' . $f;
            //$data = read_file_content($f2);
            $data = file_get_contents($f2);

            $ary = explode(chr(10), $data);
            //$dat = count($ary);
            for ($i = count($ary) - 1; $i >= 0; $i--) {
                $a2 = explode(' ', $ary[$i]);
                try {
                    if (!empty($a2) && count($a2) >= 2) {
                        
                        $t1 = $a2[0];
                        //$t1 = substr($t1,8,8));
                        $t1 = str_replace("T", " ", $t1);
                        $lab = $t1;
                        $dat = $a2[1];
                        break;
                    }
                } catch (Excepthon $e) {
                    echo $e;
                }
            }

        } catch (Exception $e) {
            $error = $e;
        }
    } else {
        $error = "文件不存在!(" . $f . ")";  //文件不存在
    }

    $div = str_replace("/", "", $div);
    $div = str_replace("\\", "", $div);
    $key = str_replace("/", "", $key);
    $key = str_replace("\\", "", $key);
    echo '{"lab":"' . $lab . '","dat":"' . $dat . '","div":"' . $div . '",'
        . '"key":"' . $key . '","error":"' . $error . '","f":"' . $f3 . '"}';
}
/*
function write_file_content($f, $data)
{
    $myfile = fopen($f, "w") or die("Unable to open file!");
    fwrite($myfile, $data);
    fclose($myfile);
}
*/
function delLine()
{
    try {
        $t = $_REQUEST["t"];
        $v = $_REQUEST["v"];
        $f = $_SESSION['f'];
        $key = $_REQUEST["key"];
        if ($key != 'wp0544') {
            echo "key errror !";
            return;
        }
        //$data = read_file_content($f);
        $data = file_get_contents($f);
        $data = str_replace($t . ' ' . $v . chr(10), '', $data);
        //write_file_content($f, $data);
        file_put_contents($f, $data);
    } catch (Exception $e) {
        echo $e;
    }
    echo 'success !';
}

function delData()
{
    try {
        $d_s = null;
        $d_e = null;
        $v_s = null;
        $v_e = null;
        
        if ( !empty($_REQUEST["v_s"]) ) $v_s = $_REQUEST["v_s"];
        if ( !empty($_REQUEST["v_e"]) ) $v_e = $_REQUEST["v_e"];
        if ( !empty($_REQUEST["d_s"]) && !empty($_REQUEST["d_e"]) ) {
            $d1 = strtotime($_REQUEST["d_s"]);
            $d2 = strtotime($_REQUEST["d_e"]);
            $d_s = date("Y-m-d", $d1) . "T" . date("H:i:s", $d1);
            $d_e = date("Y-m-d", $d2) . "T" . date("H:i:s", $d2);
        }
        
        $f = null;
        if ( !empty($_SESSION['f']) ) $f = $_SESSION['f'];
        
        if ( !empty($_REQUEST["k"]) ) {
            $k = $_REQUEST["k"];
            $f = date("Y-m", $d1) . "/" . $k . date("_Y-m", $d1) . ".txt";
            $f = getconfig("path") . $f;
            if (!file_exists($f)) {
                $f = null;
            }
        }
        
        if ( empty($f) ) return;
        
        $key = $_REQUEST["key"];
        if ($key != 'wp0544') {
            echo "key errror !";
            return;
        }
        //$data = read_file_content($f);
        $data = file_get_contents($f);
        //$data = str_replace($t . ' ' . $v . chr(10), '', $data);
        
        $lab = "";
        $dat = "";
        $ary = explode(chr(10), $data);
        //echo $f;
        //$dat = count($ary);
        echo 'find: ' . count($ary) . '; ';
        $j = 0;
        for ($i = count($ary) - 1; $i >=0; $i--) {
            $a2 = explode(' ', $ary[$i]);
            try {
                if (!empty($a2) && count($a2) >= 2) {
                    // 按开始时间和结束时间过滤
                    if ($a2[0] < $d_s || $a2[0] > $d_e)
                        continue;
                    if ((!empty($v_s) && (float)$a2[1] < (float)$v_s) || (!empty($v_e) && (float)$a2[1] > (float)$v_e))
                        continue;
                    
                    //echo $ary[$i] + '<br>';
                    $j++;
                    $data = str_replace($ary[$i] . chr(10), '', $data);
                    //$t1 = $a2[0];
                    //$t1 = str_replace("T", " ", $t1);
                    //$lab .= "'" . $t1 . "',";
                    //$dat .= $a2[1] . ",";
                    
                }
            } catch (Excepthon $e) {
                echo $e;
            }
        }        
        //write_file_content($f, $data);
        file_put_contents($f, $data);
        echo 'delete: ' . $j . '; ';
    } catch (Exception $e) {
        echo $e;
    }
    echo 'success !';
}

function setData(){
    try{
        //$d_t = $_REQUEST["d_t"];
        $key = $_REQUEST["key"];
        $val = $_REQUEST["val"];
        $dt = date("Y-m-d H:i:s");
        if(!empty($_REQUEST["dt"])){
            $dt = $_REQUEST["dt"];
            $dt = str_replace('T', ' ', $dt);
        }
        $d = strtotime($dt);
        //$f = date("Y-m") . "/" . $d_t . ".txt";
        //$f = getconfig("path_dht11") . $f;
        $dir = date("Y-m", $d);
        $f = $dir . "/" . $key . date("_Y-m", $d) . ".txt";
        $f2 = getconfig("path") . $f;

        if (!file_exists(getconfig("path") . $dir)) {
            mkdir(getconfig("path") . $dir);
        }

        $data = "";
        if (file_exists($f2)) {
            $data = file_get_contents($f2);
        }
        
        $dt2 = date("Y-m-d", $d) . "T" . date("H:i:s", $d);
        
        $data .= $dt2 . " " . $val . chr(10);
        //write_file_content($f2, $data);
        file_put_contents($f2, $data);
        echo "success!";
    } catch (Excepthon $e) {
        echo $e;
    }
    
}

function getDT()
{
    echo '{' . date("Y-m-d h:i:s a") . '}';
}

session_start();
$cmd = null;
if ( !empty($_REQUEST["cmd"]) ) $cmd = $_REQUEST["cmd"];
switch ($cmd) {
    case "getdata":
        getData();
        break;
    case "deleteline":
        delLine();
        break;
    case "deldata":
        delData();
        break;
    case "date":
        getDT();
        break;
    case "setdata":
        setData();
        break;
    case "getdatalast":
        getData_Last();
        break;
    default:
        echo "not define cmd!";
        break;
}

?>